package com.chookss.view;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chookss.R;
import com.chookss.home.rank.RankLinkEntity;
import com.chookss.tools.MyEvent;
import com.chookss.tools.Utils;
import com.chookss.view.listener.ShareDialogListener;
import com.chookss.wxapi.ShareActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

public class RankLinkDialog extends BaseDialog {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBrowse)
    TextView tvBrowse;
    @BindView(R.id.tvComplaints)
    TextView tvComplaints;
    @BindView(R.id.tvTime)
    TextView tvTime;
    @BindView(R.id.tvSure)
    TextView tvSure;
    @BindView(R.id.tvDescribe)
    TextView tvDescribe;
    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.ivStatus)
    ImageView ivStatus;
    @BindView(R.id.ivShare)
    ImageView ivShare;
    @BindView(R.id.llDescribe)
    LinearLayout llDescribe;
    private Context context;
    private RankLinkEntity rankEntity;
    private ShareDialogListener listener = null;
    private int status=-1,oldStatus=-1;//可以改变的状态  原有状态

    public RankLinkDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_rank_link;
    }

    @Override
    protected void init(View view) {
        initCenter(0.3f);
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(oldStatus!=status){
                    Map<String, String> map = new HashMap<>();
                    map.put("statusCd",status+"");
                    map.put("linkUid",rankEntity.getLinkUid());
                    EventBus.getDefault().post(new MyEvent(map));
                }
                dismiss();
            }
        });
    }

    public RankLinkDialog(@NonNull Context context, RankLinkEntity rankEntity) {
        super(context);
        this.context = context;
        this.rankEntity = rankEntity;
        tvTitle.setText(rankEntity.getResourceName());
        status=Utils.changeNumber(rankEntity.getStatusCd());
        oldStatus=Utils.changeNumber(rankEntity.getStatusCd());
        if(Utils.isNull(rankEntity.getRemarks())){
            llDescribe.setVisibility(View.GONE);
        }else{
            llDescribe.setVisibility(View.VISIBLE);
            tvDescribe.setText(rankEntity.getRemarks());
        }

        tvName.setText(rankEntity.getCreateUser());
        tvBrowse.setText(rankEntity.getViewNum());
        tvComplaints.setText(rankEntity.getTsNum());
        tvTime.setText(Utils.getAccurateDate(rankEntity.getCreateTime()));
        int statusCd=Utils.changeNumber(rankEntity.getStatusCd());
        if(statusCd==1){
            ivStatus.setBackgroundResource(R.mipmap.kaiguan_kai_icon);
        }else{
            ivStatus.setBackgroundResource(R.mipmap.kaiguan_guan_icon);
        }
        ivStatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(status==1){
                    status=0;
                    ivStatus.setBackgroundResource(R.mipmap.kaiguan_guan_icon);
                }else{
                    status=1;
                    ivStatus.setBackgroundResource(R.mipmap.kaiguan_kai_icon);
                }
            }
        });
        ivShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, ShareActivity.class);
                intent.putExtra("sourceType",1);
                intent.putExtra("rankLinkData",rankEntity);
                context.startActivity(intent);
                dismiss();
            }
        });
    }

}
